<template>
  <div class="app-container">
    <el-card>
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item label="关键字:">
          <el-input v-model="queryForm.keyword" placeholder="请输入关键字" style="width: 128px;" prefix-icon="el-icon-search"
                    size="small"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="fetchData(1)">查询</el-button>
        </el-form-item>
        <el-form-item style="flex-grow: 1;" />
        <el-form-item style="margin-right: 0;">
          <el-button type="primary" icon="el-icon-plus" size="small"
                     @click="$router.push({path:'/staffingSys/editTemplate',query:{mode:'1'}})"
          >新建
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        stripe
        highlight-current-row
      >
        <el-table-column align="center" label="工单名称" width="164">
          <template v-slot="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="工单描述">
          <template v-slot="scope">
            {{ scope.row.title }}
          </template>
        </el-table-column>
        <el-table-column class-name="status-col" label="操作" align="center" width="262">
          <template v-slot="scope">
            <!--            <el-button icon="el-icon-edit" size="mini" type="primary" @click="$router.push({path:'/workOrderSys/creatOrder',query:{name:scope.row.name}})">新建</el-button>-->
            <el-button icon="el-icon-document-copy" size="mini" type="primary" @click="openCopy(scope.row)">复制</el-button>
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="$router.push({name:'EditTemplate',params:{info:scope.row}})">编辑</el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="confirmDelete(scope.row.aid)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="width: 100%;text-align: center;margin:24px 0 0 0;">
        <el-pagination
          :page-size="pageSize"
          background
          layout="prev, pager, next"
          :current-page.sync="currentPage"
          :total="total"
          @current-change="fetchData(currentPage)"
        />
      </div>
    </el-card>
    <el-dialog title="复制模板" :visible.sync="copyDialogFormVisible">
      <el-form :model="copyForm">
        <el-form-item label="新模板名称">
          <el-input v-model="copyForm.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="copyDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="copyTemplate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteTemplate, getTemplateList } from '@/api/table'
import { PAGE_SIZES } from '@/utils/common'
import { copyTemplate } from '@/api/order'

export default {
  data() {
    return {
      copyDialogFormVisible: false,
      copyData: null,
      copyForm: {
        name: ''
      },
      list: [],
      listLoading: true,
      currentPage: 1,
      pageSize: PAGE_SIZES,
      total: 0,
      queryForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    copyTemplate() {
      const x = new FormData()
      x.append('name', this.copyForm.name)
      copyTemplate(this.copyData.aid, x).then(res => {
        this.copyDialogFormVisible = false
        this.fetchData(1)
      })
    },
    openCopy(data) {
      this.copyData = data
      this.copyDialogFormVisible = true
    },
    fetchData(page) {
      this.listLoading = true
      const params = {
        name: this.queryForm.keyword
      }
      getTemplateList(this.pageSize, page, params).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    confirmDelete(tid) {
      this.$confirm('此操作将永久删除该工单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTemplate(tid).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData(1)
        })
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-form-inline{
  display: flex;
}
</style>
